﻿using HDS.IService.Master;
using HDS.Model.Entity.Master;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HDS.Admin.Api.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    [EnableCors]
    public class PublicController : AdminController
    {
        private readonly ILogger<PublicController> _logger;
        private readonly ISupplierApiConfigService _ISupplierApiConfigService;
        private readonly IDistributorService _IDistributorConfigService;
        private readonly IGeoService _IGeoService;

        public PublicController(ILogger<PublicController> logger, ISupplierApiConfigService isupplierApiConfigService, IDistributorService idistributorConfigService, IGeoService igeoService)
        {
            _logger = logger;
            _ISupplierApiConfigService = isupplierApiConfigService;
            _IDistributorConfigService = idistributorConfigService;
            _IGeoService = igeoService;
        }

        public async Task<object> GetPublicData(string type)
        {
            switch (type)
            {
                case "sup_dis_coun":
                    var supList = await _ISupplierApiConfigService.GetList(null);
                    var supArr = supList.Select(e => e.Supplier).ToArray();

                    var disList = await _IDistributorConfigService.GetConfigList(null);
                    var disArr = disList.Select(e => e.Distributor).ToArray();

                    var counList = await _IGeoService.GetCountryList(null);
                    return new { Suppliers = supArr, Distributors = disArr, CountryList = counList };
                case "sup_dis":
                    var supList1 = await _ISupplierApiConfigService.GetList(null);
                    var supArr1 = supList1.Select(e => e.Supplier).ToArray();

                    var disList1 = await _IDistributorConfigService.GetConfigList(null);
                    var disArr1 = disList1.Select(e => e.Distributor).ToArray();

                    return new { Suppliers = supArr1, Distributors = disArr1 };
                case "coun":
                    var counList1 = await _IGeoService.GetCountryList(null);
                    return new { CountryList = counList1 };
            }

            return null;
        }

        public async Task<List<City>> GetCityList(string CountryCode)
        { 
            return await _IGeoService.GetCityList(e => e.CountryCode == CountryCode, "");
        }
    }
}
